package com.asset.salesforce.Configuration;

import java.util.Properties;

import org.apache.commons.dbcp.BasicDataSource;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.orm.hibernate4.HibernateTransactionManager;
import org.springframework.orm.hibernate4.LocalSessionFactoryBean;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

@EnableWebMvc
@Configuration
@ComponentScan(basePackages = "com.asset.salesforce")
//@ImportResource("/WEB-INF/message-converter.xml")
//@ImportResource("/WEB-INF/spring-security.xml")
@EnableTransactionManagement
public class WebConfig extends WebMvcConfigurerAdapter{

	//${jdbc.driverClassName}
  @Value("${jdbc.driverClassName}")     private String driverClassName;
  @Value("${jdbc.url}")                 private String url;
  @Value("${jdbc.username}")             private String username;
  @Value("${jdbc.password}")             private String password;
  
  @Value("${hibernate.dialect}")         private String hibernateDialect;
  @Value("${hibernate.show_sql}")     private String hibernateShowSql;
  @Value("${hibernate.hbm2ddl.auto}") private String hibernateHbm2ddlAuto;
	
 // private AnnotationMethodHandlerAdapter annotationMethodHandlerAdapter;
//  private CustomJacksonObjectMapper objectMapper;
	//<context:property-placeholder location="classpath:application.properties"></context:property-placeholder>
    @Bean
    public PropertyPlaceholderConfigurer getPropertyPlaceholderConfigurer()
    {
        PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
        ppc.setLocation(new ClassPathResource("application.properties"));
        ppc.setIgnoreUnresolvablePlaceholders(true);
        return ppc;
    }
    
    @Bean()
    public BasicDataSource getDataSource()
    {
    	BasicDataSource ds = new BasicDataSource();
        ds.setDriverClassName("org.postgresql.Driver");
        ds.setUrl("jdbc:postgresql://localhost:5432/salesforce");
        ds.setUsername("salesforce");
        ds.setPassword("salesforce");
        return ds;
    }
    @Bean
    public LocalSessionFactoryBean sessionFactory()
    {

        LocalSessionFactoryBean factoryBean = new LocalSessionFactoryBean();
        factoryBean.setDataSource(getDataSource());
        factoryBean.setHibernateProperties(getHibernateProperties());
        factoryBean.setPackagesToScan("com.asset.salesforce.model");

        return factoryBean;
    }
    
    @Bean
    public Properties getHibernateProperties()
    {
        Properties hibernateProperties = new Properties();
        hibernateProperties.setProperty("hibernate.dialect",  "org.hibernate.dialect.PostgreSQLDialect");
        hibernateProperties.setProperty("hibernate.show_sql", "true");
        hibernateProperties.setProperty("hibernate.hbm2ddl.auto", "update");
        return hibernateProperties;
    }
    
    @Bean
    public HibernateTransactionManager transactionManager(SessionFactory sessionFactory)	
    {
        HibernateTransactionManager htm = new HibernateTransactionManager();
        htm.setSessionFactory(sessionFactory);
        return htm;
    }
//    @Bean
//    public MappingJackson2HttpMessageConverter converter(){
//    	CustomJacksonObjectMapper objectMapper = new CustomJacksonObjectMapper();
//    	MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
//    	converter.setObjectMapper(objectMapper);
//    	List<MediaType>mediaTypes = new ArrayList<MediaType>();
//    	mediaTypes.add(MediaType.APPLICATION_JSON);
//    	converter.setSupportedMediaTypes(mediaTypes);
//    	return converter;
//    }
//    @PostConstruct
//    public void init(){
//    	HttpMessageConverter<?>[] messageConverters = annotationMethodHandlerAdapter.getMessageConverters();
//        for (HttpMessageConverter<?> messageConverter : messageConverters) {
//            if (messageConverter instanceof MappingJackson2HttpMessageConverter) {
//            	MappingJackson2HttpMessageConverter m = (MappingJackson2HttpMessageConverter) messageConverter;
//                m.setObjectMapper(objectMapper);
//            }
//        }
//    }
//    @Autowired
//    public void setAnnotationMethodHandlerAdapter(AnnotationMethodHandlerAdapter annotationMethodHandlerAdapter) {
//        this.annotationMethodHandlerAdapter  = annotationMethodHandlerAdapter;
//    }
//
//    @Autowired
//    public void setObjectMapper(CustomJacksonObjectMapper objectMapper) {
//        this.objectMapper = objectMapper;
//    }
//    @Bean
//    public MappingJackson2HttpMessageConverter converter(){
//    	MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
//    	HibernateAwareObjectMapper objectMapper = new HibernateAwareObjectMapper();
//    	converter.setObjectMapper(objectMapper);
//    	return converter;
//    }
//    @Bean
//    public  ViewResolver resolver() {
//        UrlBasedViewResolver url = new UrlBasedViewResolver();
//        url.setPrefix("/WEB-INF/view/");
//        url.setViewClass(JstlView.class);
//        url.setSuffix(".jsp");
//        return url;
//    }
    
}
